﻿using System;
using System.Collections.Generic;
using System.Text;
using NUnit.Framework;
using FriendsNearby.Services.Entities;
using InSTEDD.Labs.Geo;
using FriendsNearby.Services.Tests.Mocks;
using FriendsNearby.Services.Facebook;
using Facebook.Service;
using Moq;

namespace FriendsNearby.Services.Tests
{
    [TestFixture]
    public class NearbyNotificationsFixture
    {
        MockSocialNetworkService socialNetworkService;
        MockUsersService usersService;

        [SetUp]
        public void SetUp()
        {
            socialNetworkService = new MockSocialNetworkService();
            usersService = new MockUsersService();
        }

       
        [Test]
        public void NewFriendNearbyNotified()
        {
            ServiceProvider serviceProvider = new ServiceProvider();
            PeopleNearbyService peopleNearbyService = new PeopleNearbyService(serviceProvider);
            var locationService = new Mock<ILocationService>();

            serviceProvider.AddService(typeof(ISocialNetworkService), socialNetworkService);
            serviceProvider.AddService(typeof(IUsersService), usersService);
            serviceProvider.AddService(typeof(IPeopleNearbyService), peopleNearbyService);
            serviceProvider.AddService(typeof(ILocationService), new LocationService(peopleNearbyService));
                        
            LatLong myLatLongBefore = new LatLong(0, 0);
            LatLong myLatLongNow = new LatLong(0, 5);
            LatLong friendLatLongBefore = new LatLong(0, 6);
            LatLong friendLatLongNow = new LatLong(0, 6);

            locationService.Expect(x => x.SetCurrentLocation("me", myLatLongNow)); 
            locationService.Expect(x => x.SetCurrentLocation("me", myLatLongBefore));
            locationService.Expect(x => x.SetCurrentLocation("B1", friendLatLongNow));
            locationService.Expect(x => x.SetCurrentLocation("B1", friendLatLongBefore));


            UserInfo userInfoNow = new UserInfo() { Location = myLatLongNow, FacebookUserId = "me" };
            UserInfo userInfoBefore = new UserInfo() { Location = myLatLongBefore, FacebookUserId = "me" };
            UserInfo friendInfoNow = new UserInfo() { Location = friendLatLongNow, FacebookUserId = "B1" };
            UserInfo friendInfoBefore = new UserInfo() { Location = friendLatLongBefore, FacebookUserId = "B1" };

            locationService.Expect(x => x.GetCurrentLocation("me")).Returns(userInfoNow);
            locationService.Expect(x => x.GetLocationAt("me", DateTime.Today)).Returns(userInfoBefore);
            locationService.Expect(x => x.GetCurrentLocation("B1")).Returns(friendInfoNow);
            locationService.Expect(x => x.GetLocationAt("B1", DateTime.Today)).Returns(friendInfoBefore);

            locationService.Expect(x => x.GetLastNotificationDate("me")).Returns(DateTime.Today);

            socialNetworkService.GetFriends_Expected = Utils.List<string>("B1");
            socialNetworkService.GetFriendsOfFriends_Expected = Utils.List<string>();

            var nearbyDistance = LatLong.DistanceInKm(new LatLong(0, 0), new LatLong(0, 2));

            usersService.Preferences = Utils.Map<string, UserPreference>(
                "me", new UserPreference { FacebookUserId = "me", NearbyRadiusKM = (int)nearbyDistance, VisibilityLevel = (int)VisibilityLevel.Everyone },
                "B1", new UserPreference { FacebookUserId = "B1", NearbyRadiusKM = (int)nearbyDistance, VisibilityLevel = (int)VisibilityLevel.Everyone }
            );


            /*
            locationService.Value.SetCurrentLocation("me", myLatLongBefore);
            locationService.Value.SetCurrentLocation("me", myLatLongNow);

            locationService.Value.SetCurrentLocation("B1", friendLatLongBefore);
            locationService.Value.SetCurrentLocation("B1", friendLatLongNow);            
            */

            var notifications = peopleNearbyService.GetNotificationsForFacebookUser("me");
            
            foreach (var item in notifications)
            {
                Console.Out.WriteLine(item);
            }
        }
    }
}
